/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PnlEstimateBudget.java
 *
 * Created on May 4, 2010, 10:27:55 AM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.CampaignBiz;
import com.aptech.campaign.entities.Campaign;
import com.aptech.campaign.entities.EstimatedBudget;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Tai Nguyen
 */
public class EstimateBudgetPnl extends javax.swing.JPanel {

    CampaignBiz camBuz = new CampaignBiz();
    ArrayList<Campaign> arrCam = null;

    public EstimateBudgetPnl() {
        initComponents();
        putDataForCbb();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     *
     */
    private void putDataForCbb() {
        arrCam = camBuz.findByEstimate();
        cbbCampaign.removeAllItems();
        cbbCampaign.addItem("--Select Campaign--");
        for (int i = 0; i < arrCam.size(); i++) {
            cbbCampaign.addItem(arrCam.get(i).getCampaignName());
        }
    }
    /**
     *
     * @return the boolean value
     */

    private boolean valid() {
        if (cbbCampaign.getSelectedIndex() != 0 && !txfBudgetAllocation.getText().equalsIgnoreCase("") && !txfSaleVariance.getText().equalsIgnoreCase("")
                && !txfSaleAmount.getText().equalsIgnoreCase("") && !txfSaleTarget.getText().equalsIgnoreCase("")) {
            return true;
        }
        return false;
    }
    /**
     * 
     */

    private void reset() {
        txfBudgetAllocation.setText("");
        txfSaleVariance.setText("");
        txfSaleAmount.setText("");
        txfSaleTarget.setText("");
        txfBudgetVariance.setText("");
        cbbCampaign.setSelectedIndex(0);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlIcon = new javax.swing.JPanel();
        lblcon = new javax.swing.JLabel();
        pnlEstimate = new javax.swing.JPanel();
        lblCampaign = new javax.swing.JLabel();
        cbbCampaign = new javax.swing.JComboBox();
        lblSaleAmount = new javax.swing.JLabel();
        txfSaleAmount = new javax.swing.JTextField();
        lblSaletarget = new javax.swing.JLabel();
        txfSaleTarget = new javax.swing.JTextField();
        lblBudgetAllocation = new javax.swing.JLabel();
        txfBudgetAllocation = new javax.swing.JTextField();
        lblSaleVariance = new javax.swing.JLabel();
        txfSaleVariance = new javax.swing.JTextField();
        btnReset = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        lblBudgetVariance = new javax.swing.JLabel();
        txfBudgetVariance = new javax.swing.JTextField();
        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(100, 100));
        setPreferredSize(new java.awt.Dimension(100, 100));
        setLayout(new java.awt.GridBagLayout());

        pnlIcon.setMaximumSize(new java.awt.Dimension(1, 1));
        pnlIcon.setMinimumSize(new java.awt.Dimension(1, 1));
        pnlIcon.setPreferredSize(new java.awt.Dimension(200, 200));
        pnlIcon.setLayout(new java.awt.BorderLayout());

        lblcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/estimate.png"))); // NOI18N
        pnlIcon.add(lblcon, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 139;
        gridBagConstraints.ipady = 402;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.weighty = 0.8;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 0);
        add(pnlIcon, gridBagConstraints);

        pnlEstimate.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "INPUT ESTIMATE BUDGET", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlEstimate.setMinimumSize(new java.awt.Dimension(1, 1));
        pnlEstimate.setPreferredSize(new java.awt.Dimension(417, 311));
        pnlEstimate.setLayout(new java.awt.GridBagLayout());

        lblCampaign.setText("Campaign : ");
        lblCampaign.setMinimumSize(new java.awt.Dimension(100, 20));
        lblCampaign.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 26, 0);
        pnlEstimate.add(lblCampaign, gridBagConstraints);

        cbbCampaign.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Campaign--", " " }));
        cbbCampaign.setMinimumSize(new java.awt.Dimension(50, 20));
        cbbCampaign.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 99;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 26, 6);
        pnlEstimate.add(cbbCampaign, gridBagConstraints);

        lblSaleAmount.setText("Amount Product : ");
        lblSaleAmount.setMinimumSize(new java.awt.Dimension(100, 20));
        lblSaleAmount.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 26, 0);
        pnlEstimate.add(lblSaleAmount, gridBagConstraints);

        txfSaleAmount.setColumns(15);
        txfSaleAmount.setMinimumSize(new java.awt.Dimension(50, 20));
        txfSaleAmount.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 64;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 26, 6);
        pnlEstimate.add(txfSaleAmount, gridBagConstraints);

        lblSaletarget.setText("Sale target : ($)");
        lblSaletarget.setMinimumSize(new java.awt.Dimension(100, 20));
        lblSaletarget.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 26, 0);
        pnlEstimate.add(lblSaletarget, gridBagConstraints);

        txfSaleTarget.setColumns(15);
        txfSaleTarget.setMinimumSize(new java.awt.Dimension(50, 20));
        txfSaleTarget.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 64;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 26, 6);
        pnlEstimate.add(txfSaleTarget, gridBagConstraints);

        lblBudgetAllocation.setText("Budget Allocation : ($)");
        lblBudgetAllocation.setMinimumSize(new java.awt.Dimension(100, 20));
        lblBudgetAllocation.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 22;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 26, 0);
        pnlEstimate.add(lblBudgetAllocation, gridBagConstraints);

        txfBudgetAllocation.setColumns(15);
        txfBudgetAllocation.setMinimumSize(new java.awt.Dimension(50, 20));
        txfBudgetAllocation.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 64;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 26, 6);
        pnlEstimate.add(txfBudgetAllocation, gridBagConstraints);

        lblSaleVariance.setText("SaleVariance : (%)");
        lblSaleVariance.setMaximumSize(new java.awt.Dimension(300, 20));
        lblSaleVariance.setMinimumSize(new java.awt.Dimension(100, 20));
        lblSaleVariance.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 26, 0);
        pnlEstimate.add(lblSaleVariance, gridBagConstraints);

        txfSaleVariance.setColumns(15);
        txfSaleVariance.setMinimumSize(new java.awt.Dimension(50, 20));
        txfSaleVariance.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 63;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 26, 6);
        pnlEstimate.add(txfSaleVariance, gridBagConstraints);

        btnReset.setBackground(new java.awt.Color(51, 102, 255));
        btnReset.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnReset.setForeground(new java.awt.Color(255, 255, 255));
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = -24;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 40, 0, 11);
        pnlEstimate.add(btnReset, gridBagConstraints);

        btnSave.setBackground(new java.awt.Color(51, 102, 255));
        btnSave.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnSave.setForeground(new java.awt.Color(255, 255, 255));
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 28, 0, 6);
        pnlEstimate.add(btnSave, gridBagConstraints);

        lblBudgetVariance.setText("BudgetVariance : (%)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 26, 0);
        pnlEstimate.add(lblBudgetVariance, gridBagConstraints);

        txfBudgetVariance.setColumns(15);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipadx = 107;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 26, 6);
        pnlEstimate.add(txfBudgetVariance, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 254;
        gridBagConstraints.ipady = 136;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 0.8;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 0);
        add(pnlEstimate, gridBagConstraints);

        pnlTitle.setBackground(new java.awt.Color(51, 102, 255));
        pnlTitle.setMinimumSize(new java.awt.Dimension(287, 40));
        pnlTitle.setPreferredSize(new java.awt.Dimension(652, 50));
        pnlTitle.setLayout(new java.awt.BorderLayout());

        lblTitle.setBackground(new java.awt.Color(51, 102, 255));
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 30));
        lblTitle.setForeground(new java.awt.Color(51, 102, 255));
        lblTitle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/estimateBanner.jpg"))); // NOI18N
        pnlTitle.add(lblTitle, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        add(pnlTitle, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        reset();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (!valid()) {
            JOptionPane.showMessageDialog(this, "Please input data Fully!");
        } else {
            arrCam = camBuz.findByEstimate();
            EstimatedBudget etb = new EstimatedBudget();
            if (!etb.validate(txfSaleAmount.getText(), txfSaleTarget.getText(), txfBudgetAllocation.getText(), txfSaleVariance.getText(), txfBudgetVariance.getText())) {
                JOptionPane.showMessageDialog(this, etb.getLastError());
            } else {
                etb.setCampaignID(arrCam.get(cbbCampaign.getSelectedIndex() - 1).getCampaignID());
                etb.setSaleAmount(Integer.parseInt(txfSaleAmount.getText()));
                etb.setSaleTarget(Float.parseFloat(txfSaleTarget.getText()));
                etb.setBudgetAllocation(Float.parseFloat(txfBudgetAllocation.getText()));
                etb.setSaleVariance(Float.parseFloat(txfSaleVariance.getText()));
                etb.setBudgetVariance(Float.parseFloat(txfBudgetVariance.getText()));
                if (camBuz.inputEstimateBudget(etb)) {
                    JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                    putDataForCbb();
                    reset();
                } else {
                    JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                }
            }
        }
    }//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    public javax.swing.JComboBox cbbCampaign;
    private javax.swing.JLabel lblBudgetAllocation;
    private javax.swing.JLabel lblBudgetVariance;
    private javax.swing.JLabel lblCampaign;
    private javax.swing.JLabel lblSaleAmount;
    private javax.swing.JLabel lblSaleVariance;
    private javax.swing.JLabel lblSaletarget;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblcon;
    private javax.swing.JPanel pnlEstimate;
    private javax.swing.JPanel pnlIcon;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JTextField txfBudgetAllocation;
    private javax.swing.JTextField txfBudgetVariance;
    private javax.swing.JTextField txfSaleAmount;
    private javax.swing.JTextField txfSaleTarget;
    private javax.swing.JTextField txfSaleVariance;
    // End of variables declaration//GEN-END:variables
}
